/**
 *资源目录树
 */
define([
    "dojo/_base/declare",
    "esri/Graphic",
    "esri/layers/MapImageLayer",
    "esri/layers/FeatureLayer",
    "esri/layers/GraphicsLayer",
    "./js/GdpQuery.js",
], function (declare,Graphic,MapImageLayer,FeatureLayer,GraphicsLayer,GdpQuery) {

    return declare(null, {
        /**
         * 成员属性
         * @type {string}
         */
        view: Context.view,//地图视图
        _importScopeLyrTempTemp:null,
        importScopeLyr:null,
        _gdpResults:[],
        urls:{
            getAllRes:"/admin/mapres/mapres/searchAllRes"
        },

        /**
         * 获取素有通用的元素
         * (建议通过function的方式返回，防止因为缓存的原因导致元素事件无法激活)
         * @returns
         */
        elements:function(){//每次加载后均需要重新选择
            return{
            };
        },
        /**
         * 构造函数（初始化函数）
         */
        constructor: function () {
            this.init();
        },
        /**
         * 初始化函数
         */
        init:function () {
            if(this._importScopeLyrTemp==null){
                this._importScopeLyrTemp=new GraphicsLayer({
                    id:"_importScopeLyrTemp",
                    visible:true
                });
            }
            if(this.importScopeLyr==null){
                this.importScopeLyr=new GraphicsLayer({
                    id:"importScopeLyr",
                    visible:true
                });
            }
            this.initUIEvent();
        },

        initUIEvent:function(){
            var that=this;
            $("#J_selectYear").change(function () {
                var year=$("#J_selectYear").val();
                var type=$("#J_taptop_title").attr("type");
                that.getStatis(type,year);
        });
        },
        /**
         * 获取统计数据
         */
        getStatis:function (type,year) {
            var that=this;
            let gdpQuery=new GdpQuery();
            gdpQuery.startGdpQuery(type,year);//传入需要查询的年份，并开始查询
            gdpQuery.onQuerySucceed=function (results) {//查询成功回调的函数
                that._gdpResults=results;
                that.genStaTable(results);
                that.addScopeToLayerByWkt(results);
            };

        },


        /**
         * 获取统计数据格式
         */
        genStaTable:function (results){
            var that=this;
            let html='';
            for(var i=0;i<results.length;i++){
                var code=results[i].district;
                var name=results[i].district;
                var gdp=results[i].gdp;
                var index=i+1;
                html+='<tr>';
                html+='<td><div style="width: 24px;"  data-code="'+code+'">'+index+'</div></td>';
                html+='<td><div style="width: 96px;">'+name+'</div></td>';
                html+='<td><div style="width: 0px;">'+gdp+'</div></td>';
                html+='</tr>';
            }
            $('#J_dataTable tbody').html(html);

            $('#J_dataTable tbody').on( 'click', 'tr', function () {
                var  code = $(this).children(":first").children(":first").attr('data-code');
                that.locateGeo(code);
            } );
        },
        /**
         * 加载叠加显示
         * @param wkts
         */
        addScopeToLayerByWkt:function (results) {
            var that=this;
            var layer=that.importScopeLyr;
            gdbEnums=that.getResultLevel(results);
            layer.graphics.removeAll();//先移除图层
            var color='#FFA600';
            for(var i=0;i<results.length;i++){
                var geom=results[i].geom;
                var gdb=results[i].gdp;

                for(var j=0;j<gdbEnums.length;j++){
                    var _gdb=gdbEnums[j];
                    if(gdb<_gdb){
                        color=ColorEnums[j];
                        break;
                    }
                }


                // for(var j=0;j<gdbEnums.length;j++){
                //     var minGdb=gdbEnums[j];
                //     var maxGdb=gdbEnums[j+1];
                //     if(gdb>minGdb&&gdb<maxGdb){
                //         color=ColorEnums[j];
                //         break;
                //     }
                // }

                var polylineSymbol={
                    type:"simple-fill",
                    color:color,
                    outline:{
                        color:'white',
                        width:2
                    }
                };
                var pPolygonGraphic=new Graphic({
                    geometry:geom,
                    symbol:polylineSymbol
                });
                layer.add(pPolygonGraphic);
            }

            layer.visible=true;
            Context.view.map.remove(layer);
            Context.view.map.add(layer);
            Context.view.goTo(layer.graphics);
            // setTimeout(function(){
            //     var zoom=Context.view.zoom;
            //     Context.view.zoom=zoom-1;
            // }, 1000);
        },

        /**
         * 获取分级数据
         */
        getResultLevel:function(results){
            var that=this;
            var gdbs=[];
            for(var i=0;i<results.length;i++){
                var gdb=results[i].gdp;
                gdbs.push(gdb);
            }

           var max= Math.max.apply(null, gdbs);
           var min= Math.min.apply(null, gdbs);
           var gdbEnums=[];
           var index=(max-min)/10;
           var min1=(min+index).toFixed(4);
            var min2=(min+2*index).toFixed(4);
            var min3=(min+3*index).toFixed(4);
            var min4=(min+4*index).toFixed(4);
            var min5=(min+5*index).toFixed(4);
            var min6=(min+6*index).toFixed(4);
            var min7=(min+7*index).toFixed(4);
            var min8=(min+8*index).toFixed(4);
            var min9=(min+9*index).toFixed(4);
            gdbEnums.push(min1);
            gdbEnums.push(min2);
            gdbEnums.push(min3);
            gdbEnums.push(min4);
            gdbEnums.push(min5);
            gdbEnums.push(min6);
            gdbEnums.push(min7);
            gdbEnums.push(min8);
            gdbEnums.push(min9);
            gdbEnums.push(max);
            var html='';
            html+='<div class="legend_line"><div style="background:#FFFF00;"></div><span> &lt; '+min1+'</span></div>';
            html+='<div class="legend_line"><div style="background:#36FF12;"></div><span>'+min1+'-'+min2+'</span></div>';
            html+='<div class="legend_line"><div style="background:#162BFF;"></div><span>'+min2+'-'+min3+'</span></div>';
            html+='<div class="legend_line"><div style="background:#FF46A3;"></div><span>'+min3+'-'+min4+'</span></div>';
            html+='<div class="legend_line"><div style="background:#FF210B;"></div><span>'+min4+'-'+min5+'</span></div>';
            html+='<div class="legend_line"><div style="background:#70FFBD;"></div><span>'+min5+'-'+min6+'</span></div>';
            html+='<div class="legend_line"><div style="background:#ADFF58;"></div><span>'+min6+'-'+min7+'</span></div>';
            html+='<div class="legend_line"><div style="background:#FF07E6;"></div><span>'+min7+'-'+min8+'</span></div>';
            html+='<div class="legend_line"><div style="background:#6894ff;"></div><span>'+min8+'-'+min9+'</span></div>';
            html+='<div class="legend_line"><div style="background:#FF8800;"></div><span>&gt; '+min9+'</span></div>';

            $("#J_legend_line").html(html);
            $("#J_legend_subject").css("display","block");
            return gdbEnums;
        },

        /**
         * 定位
         * @param regionCode
         */
        locateGeo:function (regionCode) {
            var that = this;
            for(var i=0;i<that._gdpResults.length;i++){
                var gemo="";
                var _regionCode=that._gdpResults[i].district;
                if(regionCode==_regionCode){
                    gemo=that._gdpResults[i].geom;
                    var layer=that._importScopeLyrTemp;
                    layer.graphics.removeAll();//先移除图层
                    var color='#FFA600';
                    var polylineSymbol={
                        type:"simple-line",
                        color: 'blue',
                        width: "2px"
                        // outline:{
                        //     color:'blue',
                        //     width:2
                        // }
                    };

                    var pPolygonGraphic=new Graphic({
                        geometry:gemo,
                        symbol:polylineSymbol
                    });
                    layer.add(pPolygonGraphic);
                    layer.visible=true;
                    Context.view.map.remove(layer);
                    Context.view.map.add(layer);
                    // Context.view.goTo(layer.graphics);
                }
            }

            // var url = CommonUtils.getRegionGemoUrl() + "?gridType=SXQ&bgCode=" + regionCode + "&returnGeometry=true";
            // $.ajax({
            //     url: url,
            //     type: 'get',
            //     dataType: 'json',
            //     success: function (result) {
            //         if (result.code == 0) {
            //             var datas = result.data;
            //             if (datas.length > 0) {
            //                 for (var j = 0; j < datas.length; j++) {
            //                     var wkt = datas[j].geometry;
            //                     var layer=that._importScopeLyrTemp;
            //                     layer.graphics.removeAll();//先移除图层
            //                     // var wkt=wkts[i];
            //                     var _wkt=wkt.toLowerCase();
            //                     var color='#FFA600';
            //                     var polylineSymbol={
            //                         type:"simple-fill",
            //                         color:color,
            //                         outline:{
            //                             color:'blue',
            //                             width:2
            //                         }
            //                     };
            //                     if(wkt.indexOf("(((")>-1) {
            //                         wkt = "multipolygon" + wkt;
            //                     }else if(_wkt.indexOf("polygon")==-1){
            //                         wkt="POLYGON"+wkt;
            //                     }
            //                     var polygon=WKTToGeometry.write(wkt);
            //
            //                     var pPolygonGraphic=new Graphic({
            //                         geometry:polygon,
            //                         symbol:polylineSymbol
            //                     });
            //                     layer.add(pPolygonGraphic);
            //                     layer.visible=true;
            //                     Context.view.map.remove(layer);
            //                     Context.view.map.add(layer);
            //                     Context.view.goTo(layer.graphics);
            //                 }
            //             }
            //         }
            //     }
            // })
        }
    });
});
